<template>
  <div class="p20 bg-white console">
    <div class="mt10 mb20">
      <el-button
        class="theme-btn"
        @click="handleAdd"
        v-if="permitIds.includes('business_apart_add')"
        >新增长租公寓</el-button
      >
      <span class="ml10">
        <i class="label">商铺位置：</i>
        <AddressCascader @getRegion="handleGetRegion"></AddressCascader>
      </span>
      <span class="ml10">
        <i class="label">价格排序：</i>
        <el-select
          v-model="sort"
          @change="handleUpdate"
          clearable
          class="small-select"
        >
          <el-option label="升序" value="1"></el-option>
          <el-option label="降序" value="2"></el-option>
        </el-select>
      </span>
      <Search :placeholder="placeholder" @update="handleUpdate"></Search>
    </div>
    <el-table
      v-loading="loading"
      border
      stripe
      element-loading-text="拼命加载中"
      :data="tableData"
    >
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column prop="name" label="公寓名称"></el-table-column>
      <el-table-column
        prop="priceInterval"
        label="价格区间(元/月)"
      ></el-table-column>
      <el-table-column prop="address" label="所在地址"></el-table-column>
      <el-table-column prop="createDate" label="创建时间"></el-table-column>
      <el-table-column prop="photos" label="图片">
        <template slot-scope="scope" v-if="scope.row.photos">
          <img :src="scope.row.photos" class="detail-photo" alt />
        </template>
      </el-table-column>
      <el-table-column prop="operation" label="操作" width="350">
        <template slot-scope="scope">
          <el-button
            size="mini"
            v-if="permitIds.includes('business_apart_detail_load')"
            @click="handleDetail(scope.row.id)"
            >详情</el-button
          >
          <el-button
            size="mini"
            v-if="permitIds.includes('business_apart_detail_add')"
            @click="handleAddSon(scope.row.id)"
            >新增房型</el-button
          >
          <el-button
            size="mini"
            class="theme-btn"
            @click="handleEdit(scope.row)"
            v-if="permitIds.includes('business_apart_update')"
            >修改</el-button
          >
          <el-button
            size="mini"
            v-if="permitIds.includes('business_apart_delete')"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :pageTotal="pageTotal"
      @paginationChange="handleUpdate"
    ></Pagination>
    <ApartmentDialog
      :title="title"
      :loading="loading"
      @close="hanleClose"
      @update="handleUpdate"
      :addDialog="addDialog"
      :editDialog="editDialog"
      :apartmentForm="apartmentForm"
    ></ApartmentDialog>
    <ApartmentDetailDialog
      :title="title"
      :loading="loading"
      @close="hanleClose"
      @update="handleUpdate"
      :addDetailDialog="addDetailDialog"
    ></ApartmentDetailDialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Search from "@/components/search";
import Pagination from "@/components/pagination";
import AddressCascader from "@/components/address_cascader";
import ApartmentDialog from "./components/apartment_dialog";
import { getAllApartment, deleteApartment } from "@/apis/business";
import ApartmentDetailDialog from "./components/apartment_detail_dialog";

export default {
  name: "Apartment",
  data() {
    return {
      sort: "",
      title: "",
      pageTotal: 0,
      tableData: [],
      addDialog: false,
      editDialog: false,
      apartmentForm: {},
      addDetailDialog: false,
      placeholder: "输入公寓名称搜索"
    };
  },
  created() {
    this.$store.dispatch("load/setLoading", true);
    this.$store.dispatch("pagination/resetPageNum");
    this.getApartments(this.pageSize, this.pageNum);
  },
  computed: {
    ...mapGetters(["pageSize", "pageNum", "queryStr", "permitIds", "loading"])
  },
  methods: {
    handleGetRegion(prop) {
      this.region = prop;
      this.handleUpdate();
    },
    async getApartments(pageSize, pageNum) {
      const { object } = await getAllApartment({
        pageNum,
        pageSize,
        region: this.region,
        queryStr: this.queryStr
      });

      this.pageTotal = object.total;
      this.tableData = object.records;
      this.$store.dispatch("load/setLoading", false);
    },
    handleAdd() {
      this.title = "新增长租公寓";
      this.addDialog = !this.addDialog;
    },
    hanleClose() {
      this.addDialog = false;
      this.editDialog = false;
      this.addDetailDialog = false;
      this.$store.dispatch("load/setLoading", false);
    },
    handleUpdate() {
      this.getApartments(this.pageSize, this.pageNum);
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该条, 是否继续?", "提示", {
        type: "warning",
        cancelButtonText: "取消",
        confirmButtonText: "确定"
      })
        .then(() => {
          this.$store.dispatch("load/setLoading", true);
          // deleteApartment({ apartmentInfoId: id }).then(res => {
          //   if (res && res.code === "200") {
          //     this.$notify({
          //       title: res.msg,
          //       type: "success"
          //     });
          //     this.handleUpdate();
          //     this.$store.dispatch("load/setLoading", false);
          //   }
          // });
          this.$notify({
            title: "操作成功！",
            type: "success"
          });
          this.handleUpdate();
          this.$store.dispatch("load/setLoading", false);
        })
        .catch(() => {
          this.$message("已取消删除");
        });
    },
    handleEdit(item) {
      this.apartmentForm = item;
      this.title = "修改长租公寓";
      this.editDialog = !this.editDialog;
    },
    handleDetail(id) {
      sessionStorage.setItem("apartmentId", id);
      this.$router.push({ name: "houseType" });
    },
    handleAddSon(id) {
      this.title = "新增房型";
      sessionStorage.setItem("apartmentId", id);
      this.addDetailDialog = !this.addDetailDialog;
    }
  },
  components: {
    Search,
    Pagination,
    ApartmentDialog,
    AddressCascader,
    ApartmentDetailDialog
  }
};
</script>

<style scoped lang="less"></style>
